<template>
  <el-dialog :title="formValidate.id ? $t('BusinessConfig.machineReview.edit') : $t('BusinessConfig.machineReview.add')" :visible.sync="showState" :before-close="handleClose" :close-on-click-modal="false">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="150px" class="demo-ruleForm">
      <el-form-item :label="$t('BusinessConfig.machineReview.stageName')" prop="flowName">
        <el-input
          v-model="formValidate.flowName"
          clearable
          :maxlength="50"
          :placeholder="$t('BusinessConfig.machineReview.prompt.stageName')"
        />
      </el-form-item>
      <el-form-item v-if="formType === 1" :label="$t('BusinessConfig.machineReview.parameter')" prop="checkParameters">
        <el-select v-model="formValidate.checkParameters" multiple clearable :placeholder="$t('BusinessConfig.machineReview.prompt.parameter')">
          <el-option
            v-for="item in checkParametersData"
            :key="item.id"
            :label="item.paramName"
            :value="item.id.toString()"
          />
        </el-select>
      </el-form-item>

      <el-form-item :label="$t('BusinessConfig.machineReview.previous')" prop="previousStep">
        <el-select v-model="formValidate.previousStep" clearable :placeholder="$t('BusinessConfig.machineReview.prompt.previous')">
          <el-option
            v-for="item in selectData"
            :key="item.id"
            :label="item.flowName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('BusinessConfig.machineReview.through')" prop="nextStepPass">
        <el-select v-model="formValidate.nextStepPass" clearable :placeholder="$t('BusinessConfig.machineReview.prompt.through')">
          <el-option
            v-for="item in selectDataBy"
            :key="item.id"
            :label="item.flowName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item :label="$t('BusinessConfig.machineReview.rejection')" prop="nextStepPass">
        <el-select v-model="formValidate.nextStepRefuse" clearable :placeholder="$t('BusinessConfig.machineReview.prompt.rejection')">
          <el-option
            v-for="item in selectDataRefuse"
            :key="item.id"
            :label="item.flowName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item :label="$t('BusinessConfig.machineReview.state')" prop="status">
        <el-select v-model="formValidate.status" clearable :placeholder="$t('BusinessConfig.machineReview.prompt.state')">
          <el-option
            v-for="item in $t('BusinessConfig.machineReview.status')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addReview, updateReview } from '@/api/risk'
export default {
  name: 'RiskMachineReviewDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    formType: {
      type: Number,
      default: null
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    },
    selectDataRefuse: {
      type: Array,
      default: function() {
        return []
      }
    },
    selectDataBy: {
      type: Array,
      default: function() {
        return []
      }
    },
    selectData: {
      type: Array,
      default: function() {
        return []
      }
    },
    checkParametersData: {
      type: Array,
      default: function() {
        return []
      }
    }
  },
  data() {
    return {
      submitLoading: false,
      formValidate: {
        id: null,
        flowName: null,
        checkParameters: null,
        nextStepPass: null,
        nextStepRefuse: null,
        previousStep: null,
        status: null,
        type: null
      },
      rules: {
        flowName: [{ required: true, message: this.$t('BusinessConfig.machineReview.stageName') + this.$t('common.null'), trigger: 'blur' }],
        checkParameters: [{ required: true, message: this.$t('BusinessConfig.machineReview.parameter') + this.$t('common.null'), trigger: 'blur' }],
        previousStep: [{ required: true, message: this.$t('BusinessConfig.machineReview.previous') + this.$t('common.null'), trigger: 'blur' }],
        nextStepPass: [{ required: true, message: this.$t('BusinessConfig.machineReview.through') + this.$t('common.null'), trigger: 'blur' }],
        nextStepRefuse: [{ required: true, message: this.$t('BusinessConfig.machineReview.rejection') + this.$t('common.null'), trigger: 'blur' }],
        status: [{ required: true, message: this.$t('BusinessConfig.machineReview.state') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    handleClose() {
      this.$emit('change-dialog', false)
      this.$refs['formValidate'].clearValidate()
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          this.submitLoading = true
          const data = {
            id: this.formValidate['id'],
            flowName: this.formValidate['flowName'],
            nextStepPass: this.formValidate['nextStepPass'],
            nextStepRefuse: this.formValidate['nextStepRefuse'],
            previousStep: this.formValidate['previousStep'],
            status: this.formValidate['status'],
            type: this.formValidate['type']
          }
          if (this.formValidate['checkParameters'] && this.formType === 1) {
            data['checkParameters'] = this.formValidate['checkParameters'].join('_')
          }
          if (data['id']) {
            updateReview(data)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.editSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          } else {
            addReview(data)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.addSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-select{
    width: 100%;
  }
  .admin-tree{
    margin-top: 20px;
  }
</style>
